﻿# coding: utf-8

import os
import fnmatch
import wx

class wxPanelBrowse(wx.Panel):
    def __init__(self, parent, id):
        wx.Panel.__init__(self, parent, id, style = wx.BORDER)
        
        self._treeModel = wx.TreeCtrl(self, -1)
        
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self._treeModel, 1, wx.ALL|wx.EXPAND, border = 5)
        self.SetSizer(sizer)
        
        il = wx.ImageList(IL.IMAGE_SMALL, IL.IMAGE_SMALL)
        self._bmpFolderNormal = il.Add(IL.GetBitmap(IL.ID_IMAGE_FILE_FOLDER_CLOSE, IL.IMAGE_SMALL))
        self._bmpFolderExpand = il.Add(IL.GetBitmap(IL.ID_IMAGE_FILE_FOLDER_OPEN, IL.IMAGE_SMALL))
        self._bmpFile = il.Add(IL.GetBitmap(IL.ID_IMAGE_FILE_NORMAL, IL.IMAGE_SMALL))
        self._bmpFileSel = il.Add(IL.GetBitmap(IL.ID_IMAGE_FILE_SELECTED, IL.IMAGE_SMALL))
        self._treeModel.SetImageList(il)
        self._il = il
                
        node = self._treeModel.AddRoot(TD.WORKING_DIRECTORY)
        self._treeModel.SetItemImage(node, self._bmpFolderNormal, wx.TreeItemIcon_Normal)
        
        self._treeModel.Bind(wx.EVT_KEY_DOWN, self.__OnTreeKeyDown)
        self._treeModel.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self.__OnTreeItemActive)
        
    def __OnTreeKeyDown(self, e):
        code = e.GetKeyCode()
        if code == wx.WXK_F5:
            self.OnUpdateTreeList()
            
    def __OnTreeItemActive(self, e):
        item = e.GetItem()
        data = self._treeModel.GetItemPyData(item)
        if not data:
            return
        
        if not MODEL_EDITOR.OpenModel(data):
            dlg = wx.MessageDialog(self, TD.ERROR_OPEN_MODEL_FAILED % data, TD.ERROR_TITLE, wx.OK|wx.ICON_INFORMATION)
            dlg.ShowModal()
            dlg.Destroy()
            return
        
        wx.GetApp()._mainFrame._panelList[ID_PANEL_MESH].UpdateUI()
        wx.GetApp()._mainFrame._panelList[ID_PANEL_ANIMATION].UpdateUI()
        
    def OnUpdateTreeList(self):
        node = self._treeModel.GetRootItem()
        self._treeModel.DeleteChildren(node)
        self.WalkDirectoryTree(node, GLOBAL_SETTING._general._workDir, ['*.xml'])
        
    def WalkDirectoryTree(self, node, root, ext):
        listDir = os.listdir(root)
        for d in listDir:
            fullpath = os.path.join(root, d)
            if os.path.isdir(fullpath):
                subnode = self._treeModel.AppendItem(node, d)
                self._treeModel.SetItemImage(subnode, self._bmpFolderNormal, wx.TreeItemIcon_Normal)
                self._treeModel.SetItemImage(subnode, self._bmpFolderExpand, wx.TreeItemIcon_Expanded)
                self.WalkDirectoryTree(subnode, fullpath, ext)
            elif os.path.isfile(fullpath):
                for pat in ext:
                    if fnmatch.fnmatch(d, pat):
                        fnode = self._treeModel.AppendItem(node, d)
                        self._treeModel.SetPyData(fnode, fullpath)
                        self._treeModel.SetItemImage(fnode, self._bmpFile, wx.TreeItemIcon_Normal)
                        self._treeModel.SetItemImage(fnode, self._bmpFileSel, wx.TreeItemIcon_Selected)
                        break        
                    
                    
                    
